package com.ayguigu.order.service.impl;

import com.atguigu.order.bean.Order;
import com.atguigu.product.bean.Product;
import com.ayguigu.order.feign.ProductFeignClient;
import com.ayguigu.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * OrderServiceImpl
 *
 * @author Mars
 * @date 2025/3/14
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    RestTemplate restTemplate;

    @Autowired // 一定导入 spring-cloud-starter-loadbalancer
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    ProductFeignClient productFeignClient;

    @Override
    public Order createOrder(Long productId, Long userId) {
//        Product product = getProductFromRemoteWithLoadBalancerAnnotation(productId);

        // 使用feign完成远程调用
        Product product = productFeignClient.getProductById(productId);
        Order order = new Order();
        order.setId(1L);
        // 总金额
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserId(userId);
        order.setNickName("zhangsan");
        order.setAddress("尚硅谷");
        // 远程查询订单商品列表
        order.setProductList(Arrays.asList(product));

        return order;
    }


    // 进阶3：基于注解的负载均衡器发送请求
    private Product getProductFromRemoteWithLoadBalancerAnnotation(Long productId) {
        // 1.获取到商品服务所在的所有机器ip+port
//        ServiceInstance choose = loadBalancerClient.choose("service-product");
        // 获取ip+port 远程URL
//        String url = "http://" + choose.getHost() + ":" + choose.getPort() + "/product/" + productId;
        String url = "http://service-product/product/" + productId;
        // 2.给远程发送请求并返回
        return restTemplate.getForObject(url, Product.class);
    }


    // 进阶2：使用负载均衡器发送请求
    private Product getProductFromRemoteWithLoadBalancer(Long productId) {
        // 1.获取到商品服务所在的所有机器ip+port
        ServiceInstance choose = loadBalancerClient.choose("service-product");
        // 获取ip+port 远程URL
        String url = "http://" + choose.getHost() + ":" + choose.getPort() + "/product/" + productId;
        log.info("远程请求：{}", url);

        // 2.给远程发送请求并返回
        return restTemplate.getForObject(url, Product.class);
    }

    private Product getProductFromRemote(Long productId) {
        // 1.获取到商品服务所在的所有机器ip+port
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        // 获取第一个机器
        ServiceInstance instance = instances.get(0);
        // 获取ip+port 远程URL
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;
        log.info("远程请求：{}", url);

        // 2.给远程发送请求并返回
        return restTemplate.getForObject(url, Product.class);
    }


}